﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUS;
using WebDS;
using WebDS.CDBNames;
using IP.Core.IPCommon;
using System.Data;
using System.IO;

public partial class DanhMuc_F400_Quan_ly_items : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (m_fileUp_giai_thich.HasFile)
	    {
            m_hdf_path.Value = m_fileUp_giai_thich.PostedFile.FileName;
	    }
        

        if (m_lbl_state.Text == "0")
        {
            load_data_2_lbl_bai();
            load_data_2_m_p_noi_dung_cau();
            m_lbl_state.Text = "1";
        }
        if (!IsPostBack)        
        {
            m_hdf_index.Value = "0";
            m_lbl_stt.Text = "1";
            load_data_2_lbl_bai();
            m_hdf_index_items.Value = "0";
            load_data_2_m_p_noi_dung_cau();
            m_hdf_index_items.Value = "0";
        }     
    }

    private void load_data_2_lbl_bai()
    {
        decimal v_dc_index = new decimal();
        v_dc_index = CIPConvert.ToDecimal(m_hdf_index.Value);
        US_DM_BAI_TEST v_us_dm_bai_test = new US_DM_BAI_TEST();
        DS_DM_BAI_TEST v_ds_dm_bai_test = new DS_DM_BAI_TEST();
        v_us_dm_bai_test.FillDataset(v_ds_dm_bai_test);
        m_hdf_index_2.Value = CIPConvert.ToStr(v_ds_dm_bai_test.DM_BAI_TEST.Count);
        if ((v_dc_index >= 0) && (v_dc_index < v_ds_dm_bai_test.DM_BAI_TEST.Count))
        {
            DataTable dt = v_ds_dm_bai_test.Tables["DM_BAI_TEST"];
            DataRow dr = dt.Rows[(int)CIPConvert.ToDecimal(m_hdf_index.Value)];
            m_lbl_bai.Text = "Bài test : " + dr["BAI_TEST"].ToString();
            US_DM_CAU v_us_dm_cau = new US_DM_CAU();
            DS_DM_CAU v_ds_dm_cau = new DS_DM_CAU();
            m_hdf_id_bai_test.Value = dr["ID"].ToString();
            v_us_dm_cau.FillDatasetByIdAndSort(CIPConvert.ToDecimal(m_hdf_id_bai_test.Value), v_ds_dm_cau);
            m_lbl_tong_so_cau.Text = v_ds_dm_cau.DM_CAU.Count.ToString();
            if (m_lbl_tong_so_cau.Text == "0")
            {
                m_lbl_stt.Text = "0";
            }
            else
            {
                m_lbl_stt.Text = "1";
            }
        }
        else
        {
            m_lbl_bai.Text = "";
        }
    }

    protected void m_cmd_next_Click(object sender, ImageClickEventArgs e)
    {
        decimal index = CIPConvert.ToDecimal(m_hdf_index.Value);
        decimal count = CIPConvert.ToDecimal(m_hdf_index_2.Value);
        if ((index != 0) && (count != 0))
        {
            index = (index + 1) % count;
        }
        else
        {
            index = 1;
        }
        m_hdf_index.Value = CIPConvert.ToStr(index);
        load_data_2_lbl_bai();
        m_hdf_index_items.Value = "0";
        load_data_2_m_p_noi_dung_cau();
    }

    protected void m_cmd_prev_Click(object sender, ImageClickEventArgs e)
    {
        decimal index = CIPConvert.ToDecimal(m_hdf_index.Value);
        decimal count = CIPConvert.ToDecimal(m_hdf_index_2.Value);
        if ((index != 0) && (count != 0))
        {
            index = (index - 1) % count;
        }
        else
        {
            index = count - 1;
        }

        m_hdf_index.Value = CIPConvert.ToStr(index);
        load_data_2_lbl_bai();
        m_hdf_index_items.Value = "0";
        load_data_2_m_p_noi_dung_cau();
    }

    protected void m_cmd_next_cau_Click(object sender, ImageClickEventArgs e)
    {
        if (m_lbl_tong_so_cau.Text != "0")
        {
            decimal v_dc_stt = CIPConvert.ToDecimal(m_lbl_stt.Text);
            decimal v_dc_tong_so_cau = CIPConvert.ToDecimal(m_lbl_tong_so_cau.Text);
            v_dc_stt = v_dc_stt % v_dc_tong_so_cau + 1;
            m_lbl_stt.Text = CIPConvert.ToStr(v_dc_stt);
            m_hdf_index_items.Value = "0";
        }
        load_data_2_m_p_noi_dung_cau();
    }

    protected void m_cmd_prev_cau_Click(object sender, ImageClickEventArgs e)
    {
        if (m_lbl_tong_so_cau.Text != "0")
        {
            decimal v_dc_stt = CIPConvert.ToDecimal(m_lbl_stt.Text);
            decimal v_dc_tong_so_cau = CIPConvert.ToDecimal(m_lbl_tong_so_cau.Text);
            v_dc_stt = (v_dc_stt - 1 + v_dc_tong_so_cau - 1) % v_dc_tong_so_cau + 1;
            m_lbl_stt.Text = CIPConvert.ToStr(v_dc_stt);
            m_hdf_index_items.Value = "0";
        }
        load_data_2_m_p_noi_dung_cau();
    }

    private void load_data_2_m_p_noi_dung_cau()
    {
        if (m_lbl_tong_so_cau.Text == "0")
        {
            m_p_noi_dung_cau.Text = "";
            reset_control_in_form();
        }
        else
        {
            if (m_hdf_index_items.Value == "0")
            {
                CKEditor1.Text = "";
                US_DM_CAU v_us_dm_cau = new US_DM_CAU();
                DS_DM_CAU v_ds_dm_cau = new DS_DM_CAU();
                v_us_dm_cau.FillDatasetByIdAndSort(CIPConvert.ToDecimal(m_hdf_id_bai_test.Value), v_ds_dm_cau);
                DataTable dt = v_ds_dm_cau.Tables["DM_CAU"];
                DataRow dr = dt.Rows[(int)CIPConvert.ToDecimal(m_lbl_stt.Text) - 1];
                m_p_noi_dung_cau.Text = dr["NOI_DUNG_CAU"].ToString();
                m_hdf_id_cau.Value = dr["ID"].ToString();
                reset_control_in_form();
            }
            else
            {
                US_DM_ITEMS v_us_dm_item = new US_DM_ITEMS();
                DS_DM_ITEMS v_ds_dm_item = new DS_DM_ITEMS();
                v_us_dm_item.FillDatasetByIdAndSort(CIPConvert.ToDecimal(m_hdf_id_cau.Value), v_ds_dm_item);
                m_hdf_tong_so_items.Value = v_ds_dm_item.DM_ITEMS.Count.ToString();
                DataTable dt = v_ds_dm_item.Tables["DM_ITEMS"];
                if (dt.Rows.Count < (CIPConvert.ToDecimal(m_hdf_index_items.Value)))
                {
                    m_hdf_index_items.Value = "0";
                    load_data_2_m_p_noi_dung_cau();
                    reset_control_in_form();
                }
                else
                {
                    DataRow dr = dt.Rows[(int)CIPConvert.ToDecimal(m_hdf_index_items.Value) - 1];
                    m_p_noi_dung_cau.Text = dr["CAU_CHUA_ITEMS"].ToString();
                    m_hdf_id_item.Value = dr["ID"].ToString();
                    CKEditor1.Text = dr["GIAI_THICH"].ToString();
                    m_hdf_cau_chua_item.Value = dr["CAU_CHUA_ITEMS"].ToString();
                    //m_hdf_noi_dung.Value = dr["NOI_DUNG"].ToString();
                    m_txt_qua_trinh_td.Text = dr["NOI_DUNG"].ToString();
                    m_txt_link_htkt.Text = dr["LINK_HTKT"].ToString();
                    m_txt_tieu_de_htkt.Text = dr["TIEU_DE_HTKT"].ToString();
                }
                load_data_2_grv_case_study();
            }
        }
    }
    protected void m_cmd_next_item_Click(object sender, ImageClickEventArgs e)
    {
        m_hdf_index_items.Value = CIPConvert.ToStr(CIPConvert.ToDecimal(m_hdf_index_items.Value) + 1);
        load_data_2_m_p_noi_dung_cau();
    }
    protected void m_cmd_prev_item_Click(object sender, ImageClickEventArgs e)
    {
        if (m_hdf_index_items.Value != "0")
        {
            m_hdf_index_items.Value = CIPConvert.ToStr(CIPConvert.ToDecimal(m_hdf_index_items.Value) - 1);
        }
        load_data_2_m_p_noi_dung_cau();
    }

    protected void m_cmd_them_moi_Click(object sender, EventArgs e)
    {
        try
        {
            insert_dm_items();           
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
            m_lbl_error_items.Text = v_e.Message.ToString();
        }
        load_data_2_m_p_noi_dung_cau();
    }

    private void update_dm_item()
    {
        try
        {
            if (Page.IsValid)
            {
                if (!check_validate_is_ok()) return;
                US_DM_ITEMS v_us_dm_items= new US_DM_ITEMS();
                form_2_us_object(v_us_dm_items);
                v_us_dm_items.dcID = CIPConvert.ToDecimal(this.m_hdf_id_item.Value);
                v_us_dm_items.Update();

                reset_control_in_form();
            }
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }

    private void insert_dm_items()
    {
        try
        {
            US_DM_ITEMS v_us_dm_items = new US_DM_ITEMS();
            if (Page.IsValid)
            {
                if (!check_validate_is_ok()) return;

                form_2_us_object(v_us_dm_items);
                v_us_dm_items.Insert();
                reset_control_in_form();
            }
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }

    private void form_2_us_object(US_DM_ITEMS v_us_dm_items)
    {
        DS_DM_ITEMS ds_dm_item = new DS_DM_ITEMS();
        v_us_dm_items.FillDatasetByIdAndSort(CIPConvert.ToDecimal(m_hdf_id_cau.Value), ds_dm_item);
        if (m_hdf_index_items.Value == "0")
        {
            v_us_dm_items.dcSTT = CIPConvert.ToDecimal(ds_dm_item.DM_ITEMS.Count+1);
        }
        else
        {
            v_us_dm_items.dcSTT =CIPConvert.ToDecimal(m_hdf_index_items.Value);
        }
        m_hdf_index_items.Value = CIPConvert.ToStr(ds_dm_item.DM_ITEMS.Count + 1);
        v_us_dm_items.dcID_CAU = CIPConvert.ToDecimal(m_hdf_id_cau.Value);
        v_us_dm_items.strCAU_CHUA_ITEMS = delTag(m_p_noi_dung_cau.Text);
        //v_us_dm_items.strNOI_DUNG = m_hdf_noi_dung.Value;
        v_us_dm_items.strNOI_DUNG = delTag(m_txt_qua_trinh_td.Text);
        v_us_dm_items.strGIAI_THICH = delTag(CKEditor1.Text);
        v_us_dm_items.strTIEU_DE_HTKT =m_txt_tieu_de_htkt.Text;
        v_us_dm_items.strLINK_HTKT =m_txt_link_htkt.Text;
    }

    private string delTag(string ip_str)
    {
        string s = ip_str;
        s = s.Replace("<p>"," ");
        s = s.Replace("</p>"," ");
        s.Trim();
        return s;
    }

    private void reset_control_in_form()
    {
        CKEditor1.Text = "";
        m_txt_tieu_de_htkt.Text = "";
        m_txt_link_htkt.Text = "";
    }

    private bool check_validate_is_ok()
    {
        return true;
    }

    protected void m_cmd_tim_kiem_Click(object sender, EventArgs e)
    {
        load_data_2_grid_tim_kiem();
    }

    private void load_data_2_grid_tim_kiem()
    {
        US_V_DM_ITEMS v_us_dm_item = new US_V_DM_ITEMS();
        DS_V_DM_ITEMS v_ds_dm_item = new DS_V_DM_ITEMS();

        v_us_dm_item.FillDatasetSearch(v_ds_dm_item, m_txt_tim_kiem.Text);
        m_grv_tim_kiem.DataSource = v_ds_dm_item.V_DM_ITEMS;
        m_grv_tim_kiem.DataBind();
    }

    protected void m_cmd_chon_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow row in m_grv_tim_kiem.Rows)
            {
                bool isChecked = ((CheckBox)row.FindControl("chkItem")).Checked;
                if (isChecked)
                {
                    decimal v_dc_id_items_tuong_ung = CIPConvert.ToDecimal(((CheckBox)row.FindControl("chkItem")).ToolTip);
                    insert_dm_case_study(v_dc_id_items_tuong_ung);
                }
            }
        }
        catch (Exception)
        {
            m_lbl_error.Text = "Đã xảy ra lỗi trong quá trình Lưu Case Study";
            throw;
        }        
    }

    private void insert_dm_case_study(decimal v_dc_id_items_tuong_ung)
    {
        US_DM_CASE_STUDY v_us_dm_cs = new US_DM_CASE_STUDY();
        DS_DM_CASE_STUDY v_ds_dm_cs = new DS_DM_CASE_STUDY();
        v_us_dm_cs.FillDatasetByIdItem(v_ds_dm_cs, CIPConvert.ToDecimal(m_hdf_id_item.Value));
        DataTable dt = v_ds_dm_cs.Tables["DM_CASE_STUDY"];
        decimal count = dt.Rows.Count;
        if (Page.IsValid)
        {
            if (!check_validate_is_ok()) return;
            v_us_dm_cs.dcID_ITEMS = CIPConvert.ToDecimal(m_hdf_id_item.Value);
            v_us_dm_cs.dcID_ITEMS_TUONG_UNG = v_dc_id_items_tuong_ung;
            v_us_dm_cs.dcSTT = count + 1;
            v_us_dm_cs.Insert();
            load_data_2_grv_case_study();
        }
    }

    private void load_data_2_grv_case_study()
    {
        US_V_DM_CASE_STUDY_2 v_us_v_dm_case_study = new US_V_DM_CASE_STUDY_2();
        DS_V_DM_CASE_STUDY_2 v_ds_v_dm_case_study = new DS_V_DM_CASE_STUDY_2();

        v_us_v_dm_case_study.FillDatasetByIdItem(v_ds_v_dm_case_study, CIPConvert.ToDecimal(m_hdf_id_item.Value));

        m_grv_dm_case_study.DataSource = v_ds_v_dm_case_study.V_DM_CASE_STUDY_2;
        m_grv_dm_case_study.DataBind();
    }
    protected void m_grv_dm_case_study_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            delete_dm_case_study(e.RowIndex);
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
            m_lbl_error_items.Text = v_e.Message.ToString();
        }
    }

    private void delete_dm_case_study(int i_int_row_index)
    {
        try
        {
            US_DM_CASE_STUDY v_us_dm_case_study = new US_DM_CASE_STUDY();
            decimal v_dc_id_dm_tu_dien = CIPConvert.ToDecimal(m_grv_dm_case_study.DataKeys[i_int_row_index].Value);
            v_us_dm_case_study.DeleteByID(v_dc_id_dm_tu_dien);
            load_data_2_grv_case_study();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }

    protected void m_grv_dm_case_study_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        UpRow(e.NewSelectedIndex);
        return;
    }

    private void UpRow(int i_int_row_index)
    {
        if (i_int_row_index == 0)
        {
            return;
        }
        US_DM_CASE_STUDY v_us_dm_case_study = new US_DM_CASE_STUDY();
        DS_DM_CASE_STUDY v_ds_dm_case_study = new DS_DM_CASE_STUDY();
        v_us_dm_case_study.FillDatasetByIdItem(v_ds_dm_case_study, CIPConvert.ToDecimal(m_hdf_id_item.Value));
        DataTable dt = v_ds_dm_case_study.Tables["DM_CASE_STUDY"];
        DataRow dr = dt.Rows[i_int_row_index];
        decimal row_stt =CIPConvert.ToDecimal(dr["STT"].ToString());
        decimal row_id = CIPConvert.ToDecimal(dr["ID"].ToString());

        DataRow dr_up = dt.Rows[i_int_row_index-1];
        decimal row_stt_up = CIPConvert.ToDecimal(dr_up["STT"].ToString());
        decimal row_id_up = CIPConvert.ToDecimal(dr_up["ID"].ToString());

        v_us_dm_case_study.dcID = row_id;
        v_us_dm_case_study.dcID_ITEMS = CIPConvert.ToDecimal(dr["ID_ITEMS"].ToString());
        v_us_dm_case_study.dcID_ITEMS_TUONG_UNG = CIPConvert.ToDecimal(dr["ID_ITEMS_TUONG_UNG"].ToString());
        v_us_dm_case_study.dcSTT = row_stt_up;
        v_us_dm_case_study.Update();

        v_us_dm_case_study.dcID = row_id_up;
        v_us_dm_case_study.dcID_ITEMS = CIPConvert.ToDecimal(dr_up["ID_ITEMS"].ToString());
        v_us_dm_case_study.dcID_ITEMS_TUONG_UNG = CIPConvert.ToDecimal(dr_up["ID_ITEMS_TUONG_UNG"].ToString());
        v_us_dm_case_study.dcSTT = row_stt;
        v_us_dm_case_study.Update();

        load_data_2_grv_case_study();
    }
    protected void down_click(object sender, GridViewCommandEventArgs e)
    {
        return;
    }
    protected void m_grv_dm_case_study_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        downRow(e.RowIndex);
        return;
    }

    private void downRow(int i_int_row_index)
    {
        
        US_DM_CASE_STUDY v_us_dm_case_study = new US_DM_CASE_STUDY();
        DS_DM_CASE_STUDY v_ds_dm_case_study = new DS_DM_CASE_STUDY();
        v_us_dm_case_study.FillDatasetByIdItem(v_ds_dm_case_study, CIPConvert.ToDecimal(m_hdf_id_item.Value));

        

        DataTable dt = v_ds_dm_case_study.Tables["DM_CASE_STUDY"];

        if (i_int_row_index + 1 == dt.Rows.Count)
        {
            return;
        }

        DataRow dr = dt.Rows[i_int_row_index];
        decimal row_stt = CIPConvert.ToDecimal(dr["STT"].ToString());
        decimal row_id = CIPConvert.ToDecimal(dr["ID"].ToString());

        DataRow dr_up = dt.Rows[i_int_row_index + 1];
        decimal row_stt_up = CIPConvert.ToDecimal(dr_up["STT"].ToString());
        decimal row_id_up = CIPConvert.ToDecimal(dr_up["ID"].ToString());

        v_us_dm_case_study.dcID = row_id;
        v_us_dm_case_study.dcID_ITEMS = CIPConvert.ToDecimal(dr["ID_ITEMS"].ToString());
        v_us_dm_case_study.dcID_ITEMS_TUONG_UNG = CIPConvert.ToDecimal(dr["ID_ITEMS_TUONG_UNG"].ToString());
        v_us_dm_case_study.dcSTT = row_stt_up;
        v_us_dm_case_study.Update();

        v_us_dm_case_study.dcID = row_id_up;
        v_us_dm_case_study.dcID_ITEMS = CIPConvert.ToDecimal(dr_up["ID_ITEMS"].ToString());
        v_us_dm_case_study.dcID_ITEMS_TUONG_UNG = CIPConvert.ToDecimal(dr_up["ID_ITEMS_TUONG_UNG"].ToString());
        v_us_dm_case_study.dcSTT = row_stt;
        v_us_dm_case_study.Update();

        load_data_2_grv_case_study();
    }
    protected void m_cmd_upload_giai_thich_Click(object sender, EventArgs e)
    {
        if (m_fileUp_giai_thich.PostedFile != null)
        {
            if (m_fileUp_giai_thich.HasFile)
            {
                string FileName = Path.GetFileName(m_fileUp_giai_thich.PostedFile.FileName);
                string path = Server.MapPath("../Images/Upload/" + FileName);
                m_fileUp_giai_thich.SaveAs(path);
                CKEditor1.Text = "<p>" + delTag(CKEditor1.Text) + "<br><img src='../Images/Upload/" + FileName + "' /></p>";        
            }            
        }        
    }
    protected void m_cmd_sua_item_Click(object sender, EventArgs e)
    {
        try
        {
            update_dm_item();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
            m_lbl_error_items.Text = v_e.Message.ToString();
        }
        load_data_2_m_p_noi_dung_cau();
    }
    protected void m_cmd_xoa_item_Click(object sender, EventArgs e)
    {
        US_DM_ITEMS v_us_dm_item = new US_DM_ITEMS();
        decimal dc_id_item = CIPConvert.ToDecimal(m_hdf_id_item.Value);
        v_us_dm_item.DeleteByID(dc_id_item);
        load_data_2_m_p_noi_dung_cau();
    }
    protected void m_grv_tim_kiem_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            m_grv_tim_kiem.PageIndex = e.NewPageIndex;
            load_data_2_grid_tim_kiem();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
}